package com.itbaizhan.openhis.controller.doctor;

import cn.hutool.core.bean.BeanUtil;
import com.itbaizhan.openhis.constant.Constants;
import com.itbaizhan.openhis.domain.CareHistory;
import com.itbaizhan.openhis.domain.CareOrder;
import com.itbaizhan.openhis.domain.CareOrderItem;
import com.itbaizhan.openhis.service.CareHistoryService;
import com.itbaizhan.openhis.vo.AjaxResult;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("doctor/handleMedicine")
public class HandleMedicinesController {
    @Reference
    private CareHistoryService careHistoryService; //病历

    /**
     *根据挂号单id查询已支付的处方订单
     * @param regId 挂号id
     * @return
     */
    @GetMapping("getChargedCareHistoryOnlyMediciByRegId/{regId}")
    public AjaxResult getChargedCareHistoryOnlyMediciByRegId(@PathVariable String regId){
        CareHistory careHistory = careHistoryService.queryCareHistoryByRegId(regId);
        if(careHistory == null ){
            return AjaxResult.fail("【"+regId+"】对应的遍历不存在，请核对后再查询");
        }
        //把病历与处方封装到map
        HashMap<String,Object> res = new HashMap<>();
        res.put("careHistory",careHistory);//添加病历
        res.put("careOrder", Collections.EMPTY_LIST); //先放一个空的，下面遍历了现添加
        //定义一个存放处方的集合，处方有很多种，例：检查处方，药品处方
        ArrayList<Map<String,Object>> mapList = new ArrayList<>();
        //根据病历编号查处方，一个病历id有多个处方
        List<CareOrder> careOrders = careHistoryService.queryCareOrdersByChId(careHistory.getChId());
        if(careOrders.isEmpty()){
            return AjaxResult.fail("【"+regId+"】没有相对应的处方信息，请核对后再查询");
        }
        //遍历处方，再遍历处方里的详情
        for(CareOrder careOrder:careOrders) {
            //判断是药品处方才进行处理
            if (careOrder.getCoType().equals(Constants.CO_TYPE_MEDICINES)) {
                Map<String, Object> beanToMap = BeanUtil.beanToMap(careOrder);
                BigDecimal allAmount = new BigDecimal("0"); //计算处方价格
                //根据处方id查处方详情
                List<CareOrderItem> careOrderItems = careHistoryService.queryCareOrderItemsByCoId(careOrder.getCoId(), Constants.ORDER_STATUS_0);
                if (careOrderItems.isEmpty()) {
                    continue; //结束
                } else {
                    for (CareOrderItem careOrderItem : careOrderItems) {
                        allAmount = allAmount.add(careOrderItem.getAmount());
                    }
                    beanToMap.put("allAmount", allAmount);
                    beanToMap.put("careOrderItems", careOrderItems);
                    mapList.add(beanToMap);
                }

            }
        }
        if(mapList.isEmpty()){
            return AjaxResult.fail("【"+regId+"】挂号单没有已支付的药品处方信息，请核对后再查询");
        }else {
            res.put("careOrder",mapList); //添加处方
            return AjaxResult.success(res);
        }
    }

    /**
     * 发药
     * @param itemIds
     * @return
     */
    @PostMapping("doMedicine")
    public AjaxResult doMedicine(@RequestBody List<String> itemIds){
        //首先判断是否为空，发药后，要更改处方明细，收费明细的状态为已完成，还有库存的减小
        if(itemIds == null || itemIds.isEmpty()){
            return AjaxResult.fail("请选择要发药的药品项");
        }
        String msg = careHistoryService.doMedicine(itemIds);

        if(StringUtils.isNotBlank(msg)){
            //如果还有药品信息，证明药品还没有发出
            return AjaxResult.fail(msg);
        }else {
            return AjaxResult.success();
        }
    }
}
